package com.qf.business.user.core.controller;

import com.qf.business.user.core.service.WxScoreDetailsService;
import com.qf.business.user.core.service.WxUserService;
import com.qf.commons.data.result.R;
import com.qf.commons.web.aspect.annotation.GetUser;
import com.qf.commons.web.utils.UserUtils;
import com.qf.data.user.entity.WxScoreDetails;
import com.qf.data.user.entity.WxUser;
import com.qf.data.user.vo.input.WxScoreUpdateInput;
import com.qf.data.user.vo.input.WxUserInput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/wx")
public class WxUserController {

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private WxScoreDetailsService wxScoreDetailsService;

    /**
     * 查询微信的用户信息
     * @return
     */
    @RequestMapping("/query")
    public R<WxUser> queryWxUser(@RequestBody WxUserInput wxUserInput){
        WxUser wxUser = wxUserService.login(wxUserInput);
        return R.create(wxUser);
    }

    /**
     * 查询微信小程序用户的积分
     * @return
     */
    @RequestMapping("/score/query")
    @GetUser
    public R queryWxScore(){
        //获取登录的用户id
        Integer uid = UserUtils.getUser().getId();
        WxUser users = wxUserService.getById(uid);
        return R.create(users);
    }

    /**
     * 积分修改
     * @return
     */
    @RequestMapping("/score/update")
    public R<Integer> updateWxScore(@RequestBody WxScoreUpdateInput scoreUpdateInput){
        int update = wxUserService.updateWxScore(scoreUpdateInput);
        return R.create(update);
    }

    /**
     * 生成积分流水
     * @return
     */
    @RequestMapping("/score/details")
    public R<Integer> createScoreDetails(@RequestBody WxScoreDetails scoreDetails){
        wxScoreDetailsService.save(scoreDetails);
        return R.create(1);
    }
}